//
//  ForgetPassWordViewController.m
//  zhuchiren
//
//  Created by rainday on 15/1/10.
//  Copyright (c) 2015年 rainday. All rights reserved.
//

#import "ForgetPassWordViewController.h"
#import <SMS_SDK/SMS_SDK.h>
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "MeViewModel.h"
#import "NetErrorStateCode.h"
#import <MBProgressHUD.h>

@interface ForgetPassWordViewController ()<UIAlertViewDelegate>

@property (nonatomic ,weak) IBOutlet UIButton *getVerButton;
@property (nonatomic ,weak) IBOutlet UIButton *resetButton;

@property (nonatomic ,weak) IBOutlet UITextField *verTextField;

@property (nonatomic ,weak) IBOutlet UITextField *aNewPassWordTextField;
@property (nonatomic ,weak) IBOutlet UITextField *againPassWordTextField;

- (IBAction)resetPassWord:(id)sender;

- (IBAction)dismissSelf:(id)sender;


@end

@implementation ForgetPassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _getVerButton.layer.masksToBounds = YES;
    _getVerButton.layer.cornerRadius = 3.0f;
    
    _resetButton.layer.cornerRadius = 5.0f;
    _resetButton.layer.masksToBounds = YES;
    @weakify(self);
    
    
    RAC(_resetButton,enabled) = [RACSignal combineLatest:@[_aNewPassWordTextField.rac_textSignal,_againPassWordTextField.rac_textSignal,_verTextField.rac_textSignal] reduce:^id(NSString *passwd,NSString *againPasswd,NSString *verString){
        return @([passwd length]>0 && [passwd isEqualToString:againPasswd] && [verString length]==4);
    }];
    
    RAC(_resetButton,backgroundColor) = [RACSignal combineLatest:@[_aNewPassWordTextField.rac_textSignal,_againPassWordTextField.rac_textSignal,_verTextField.rac_textSignal] reduce:^id(NSString *passwd,NSString *againPasswd,NSString *verString){
        return [passwd length]>0 && [passwd isEqualToString:againPasswd] && [verString length]==4?[UIColor redColor]:[UIColor grayColor];
    }];

    
    
    [[_getVerButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       
        [SMS_SDK getVerificationCodeBySMSWithPhone:_phoneString
                                              zone:@"86"
                                            result:^(SMS_SDKError *error)
         {
             if (!error)
             {
                 
                 UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"发送成功", nil)
                                                               message:@"注意查收"
                                                              delegate:nil
                                                     cancelButtonTitle:NSLocalizedString(@"好的", nil)
                                                     otherButtonTitles:nil, nil];
                 [alert show];
                 
             }
             else
             {
                 UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"验证码填写错误", nil)
                                                               message:[NSString stringWithFormat:@"状态码：%zi ,错误描述：%@",error.errorCode,error.errorDescription]
                                                              delegate:nil
                                                     cancelButtonTitle:NSLocalizedString(@"sure", nil)
                                                     otherButtonTitles:nil, nil];
                 [alert show];
             }
             
         }];

    }];
    
    
    [[_resetButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       
        @strongify(self);
        [SMS_SDK commitVerifyCode:_verTextField.text result:^(enum SMS_ResponseState state) {
            if (1==state)
            {
                
                
                
                [[MeViewModel forgetForPhone:_phoneString password:_aNewPassWordTextField.text] subscribeNext:^(id x) {
                
                    UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"修改成功", nil)
                                                                  message:@"已帮您重置密码"
                                                                 delegate:self
                                                        cancelButtonTitle:NSLocalizedString(@"好的", nil)
                                                        otherButtonTitles:nil, nil];
                    [alert show];
                    
                } error:^(NSError *error) {
                    NSString *code = [NetErrorStateCode getDescribeText:error];
                    if ([code isEqualToString:@"未找到"]) {
                        UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"重置失败", nil)
                                                                      message:@"该手机号码尚未注册"
                                                                     delegate:self
                                                            cancelButtonTitle:NSLocalizedString(@"好的", nil)
                                                            otherButtonTitles:nil, nil];
                        [alert show];
                        
                        
                        
                    }
                }];
                
                
            }
            else if(0==state)
            {
                NSString* str=[NSString stringWithFormat:NSLocalizedString(@"verifycodeerrormsg", nil)];
                UIAlertView* alert=[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"verifycodeerrortitle", nil)
                                                              message:str
                                                             delegate:self
                                                    cancelButtonTitle:NSLocalizedString(@"sure", nil)
                                                    otherButtonTitles:nil, nil];
                [alert show];
            }
        }];
        

    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)dismissSelf:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)resetPassWord:(id)sender{

    [self dismissViewControllerAnimated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
